//
//  RequestProRank.m
//  SAT_NET
//
//  Created by lanou3g on 16/3/7.
//  Copyright © 2016年 郭丹. All rights reserved.
//

#import "RequestProRank.h"
#import "DetailAwardModel.h"
#import "DetailModel.h"

@implementation RequestProRank

- (void)requestWithUrl:(NSString *)url
                 success:(success)success
                    fail:(fail)fail {
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
        NSURLSession *session = [NSURLSession sharedSession];
        NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            NSMutableArray *arrayAllData = [NSMutableArray array];
            NSString *allNum = nil;
            if (data) {
                
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                allNum = dic[@"allNum"];
                
                for (NSDictionary *dicTem in dic[@"data"]) {
                    if ([dicTem.allKeys containsObject:@"award"] == YES) {
                        DetailAwardModel *detailAM = [[DetailAwardModel alloc] init];
                        [detailAM setValuesForKeysWithDictionary:dicTem];
                        [arrayAllData addObject:detailAM];
                    } else {
                        DetailModel *detailM = [[DetailModel alloc] init];
                        [detailM setValuesForKeysWithDictionary:dicTem];
                        [arrayAllData addObject:detailM];
                    }
                }
                
            } else {
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (success) {
                        success(nil,nil);
                    }
                });
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                if (success) {
                    success(arrayAllData,allNum);
                }
            });
            
            
            
        }];
        [task resume];
    });
    
    
}

@end
